<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use Illuminate\Validation\Rule;

class SystemConfigController extends Controller
{
    use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\SystemConfig::class);
    }

    protected function getAttributes(){
        return [
            'identity' => '标识',
            'deviation' => '偏移值',
            'upper_limit' => '开始时间',
            'lower_limit' => '结束时间',
            'desc' => '描述',
            'unit' => '单位'
        ];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return formatJsonResponse($this->pageModel(request()->input()));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rules = [
            'identity' => [
                'required',
                Rule::unique('system_config','identity')->where(function ($query) use ($id){
                    $query->where('id','<>', $id);
                })
            ],
            'upper_limit' => 'nullable',
            'lower_limit' => 'nullable',
            'desc' => 'required',
            'unit' => 'required'
        ];

        if($request->identity == 'ex_weight_error' || $request->identity == 'in_weight_error'){
            $rules['unit'] = 'required|numeric';
        }

        $this->validate($rules);

        $this->updateModel($id,$request->all());

        return formatJsonResponse();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    public function shiBang(){
        $systemConfig = $this->model()->where('identity','trial_weight')->first();
        
        if($systemConfig->deviation == '0'){
            $systemConfig->deviation = 1;

            $systemConfig->save();
        }
        else{
            $systemConfig->deviation = 0;

            $systemConfig->save();
        }

        return formatJsonResponse();
    }
}
